<template>
  <div class="dialog">
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item label="车商名称">
        <el-input placeholder="请输车商名称" v-model="params.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="params.flag">
          <el-option label="全部" value=""></el-option>
          <el-option label="审核通过" value="1"></el-option>
          <el-option label="未审核" value="0"></el-option></el-select>
      </el-form-item>
      <el-form-item label="销售名称">
        <el-select v-model="params.partnerCode">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="item in partners" :key="item.code" :label="item.name" :value="item.code">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" style="margin-left: 100px">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%" v-loading="loading" size="small">
      <el-table-column prop="id" label="序号" align="center" width="90"> </el-table-column>
      <el-table-column prop="dealerName" label="车商名称" align="center"></el-table-column>
      <el-table-column label="车商保证金(总)" align="center" width="80">
        <template slot-scope="scope">
          <span style="color: #67C23A;">{{scope.row.money}}</span>          
        </template>
      </el-table-column>
      <el-table-column label="车商保证金(冻结)" align="center" width="100">
        <template slot-scope="scope">
          <span style="color: #F56C6C;">{{ scope.row.lockedMoney }}</span>          
        </template>
      </el-table-column>
      <el-table-column label="车商地址" align="center">
        <template slot-scope="scope">
          {{ scope.row.provinceName }}   {{ scope.row.cityName }}   {{ scope.row.countyName }}   {{ scope.row.address }}       
        </template>
      </el-table-column>
      <el-table-column prop="contacts" label="联系人" align="center" width="100"></el-table-column>
      <el-table-column prop="mobile" label="联系电话" align="center" width="100"></el-table-column>
      <el-table-column prop="partnerName" label="销售" align="center" width="100"></el-table-column>
      <el-table-column prop="flag" label="车商状态" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.flag == 1" style="color: #67C23A;">审核通过</span>
          <span v-if="scope.row.flag == 0" style="color: #F56C6C;">未审核</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="150"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onEditShop(scope.row)">编辑</el-button>
          <!-- <el-button type="text" v-if="scope.row.flag === 1" size="mini" @click="onModifyStatus(scope.row, 0)"></el-button> -->
          <el-button type="text" v-if="scope.row.flag === 0" size="mini" @click="onModifyStatus(scope.row, 1)">审核通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="float: left; margin-top: 20px">
      <div>
        <span style="
            padding-top: 10px;
            display: inline-block;
            vertical-align: middle;
            color: #606266;
            font-size: 14px;
          ">显示第{{ this.params.page }}页的{{ this.params.rows }}条记录，总共{{
            this.total
          }}条记录
        </span>
      </div>
    </div>
    <div class="block" style="float: right; margin-top: 20px">
      <el-pagination :current-page.sync="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.rows" background
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>

    <el-dialog :visible.sync="editShopVisible" title="添加车商" width="800px" center>
      <el-form :model="shopData" ref="from" label-width="150px">
        <el-form-item label="名称:">
          <el-input v-model="shopData.dealerName"></el-input>
        </el-form-item>
        <el-form-item label="地址:">
          <el-input v-model="shopData.address"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="shopData.mobile"></el-input>
        </el-form-item>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editShopVisible = false">关 闭</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="modifyVisible" title="提示" width="450px" center>
      <div class="del-dialog-cnt">该车商确定审核通过？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleModifyStatus">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import {
  getBusinessList,updateBussinessStatus,editBusiness
} from "@/api/carBusiness";
import {
  getAllPartnerList
} from "@/api/user";
import _ from 'lodash';
export default {
  data() {
    return {
      loading: false,
      currentPage4: 4,
      total: 0,
      params: {
        name: '',
        flag:'',
        partnerCode:''
      },
      tableData: [],
      partners:[],
      editShopVisible: false,
      modifyVisible: false,
      shopData: {
        id: 0,
        dealerName: '',
        mobile: '',
        address: '',
      }
    };
  },

  methods: {
    onSearch() {
      this.getData();
    },
    handleSizeChange(val) {
      this.params.rows = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.getData();
    },
    getPartner(){
      getAllPartnerList().then((res) => {
          this.partners = res.data;
        });
    },
    getData(exportExcel) {
      if (exportExcel) {
        this.params.export = true
      } else {
        this.loading = true;
        let params = {
          pageSize: this.params.rows,
          pageNo: this.params.page,
          filter: [{
            field: 'dealerName',
            operator: 'LIKE',
            value: this.params.name
          }],
          orderType:'desc',
          orderField:'id'
        }
        if(this.params.flag!=''){
          params.filter.push({
            field: 'flag',
            operator: 'EQ',
            value: this.params.flag
          })
        }
        if(this.params.partnerCode!=''){
          params.filter.push({
            field: 'partnerCode',
            operator: 'EQ',
            value: this.params.partnerCode
          })
        }
        getBusinessList(params).then((res) => {
          this.loading = false;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        });
      }
    },
    onExport() {
      this.getData(true)
    },
    onEditShop(row) {
      this.resetForm();
      this.editShopVisible = true;
      this.shopData = _.clone(row);
    },
    handleEdit() {
      editBusiness(this.shopData).then((res) => {
        this.getData();
        this.editShopVisible = false;
      });
    },
    onModifyStatus(row, flag) {
      this.modifyVisible = true;
      this.shopData.id = row.id;
      this.shopData.flag = flag;
    },
    handleModifyStatus() {
      updateBussinessStatus(this.shopData.id, this.shopData.flag).then((res) => {
        this.getData();
        this.modifyVisible = false;
      });
    },
    resetForm() {
      this.shopData = {
        id: 0,
        dealerName: '',
        mobile: '',
        address: '',
      }
    }
  },
  mounted() {
    this.getData();
    this.getPartner()
  },
};
</script>
<style scoped lang="scss">
.my-radio {
  margin-bottom: 10px;
}

.dialog {
  width: 95%;
  height: 100%;
  padding-top: 10px;
  margin-left: 10px;
}
</style>
